"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const FileStreamRotator = __importStar(require("file-stream-rotator"));
const fs_1 = require("fs");
const path = __importStar(require("path"));
const util = __importStar(require("util"));
const LoggerWriter_1 = require("./LoggerWriter");
class FileLoggerWriter extends LoggerWriter_1.DefaultLoggerWriter {
    constructor(name, options = {}) {
        super(name, options);
        this.options = options;
        this.dirname = './logs';
        if (typeof options.dirname === 'string') {
            this.dirname = options.dirname;
        }
        let rotateFormat = options.rotateFormat;
        if (rotateFormat === false) {
            this.stream = fs_1.createWriteStream(path.join(this.dirname, `${this.name}.log`), {
                flags: 'a',
                encoding: 'utf8',
            });
        }
        else {
            if (typeof rotateFormat === 'undefined') {
                rotateFormat = 'YYYYMMDD';
            }
            this.stream = FileStreamRotator.getStream({
                filename: path.join(this.dirname, `${this.name}-%DATE%.log`),
                frequency: 'custom',
                date_format: rotateFormat,
                verbose: false,
                audit_file: path.join(this.dirname, `${this.name}-audit.json`),
                size: '100m',
                end_stream: true,
            });
        }
    }
    format(methodName, timestamp) {
        return `${this.formatLevel(methodName)} ${this.formatTime(timestamp)}`;
    }
    write(methodName, timestamp, ...msg) {
        const prefix = this.format(methodName, timestamp);
        const [first] = msg;
        if (typeof first === 'string') {
            msg[0] = `${prefix} ${first}`;
        }
        else {
            msg.unshift(prefix);
        }
        if (methodName === 'trace') {
            msg.push(`\n${this.getTraceStack()}`);
        }
        this.stream.write(util.format(...msg) + '\n');
    }
    formatTime(time) {
        return `[${super.formatTime(time)}]`;
    }
    formatLevel(methodName) {
        return super.formatLevel(methodName)[0];
    }
}
exports.FileLoggerWriter = FileLoggerWriter;
